package gateway

import (
	"context"
	"gitee.com/wayjin/fabric-extension/cryptoutil"
	"github.com/golang/protobuf/proto"
	"github.com/hyperledger/fabric/protos/common"
	pb "github.com/hyperledger/fabric/protos/peer"
	"log"
)

type PeerStatus string

func getPeerStatus(suite cryptoutil.CryptoSuite, peerCli *peerClient) (PeerStatus, error) {
	signer, err := suite.NewSigner()
	if err != nil {
		return "", err
	}
	wrapEnv := func(msg proto.Message) *common.Envelope {
		env, err := createSignedEnvelop(common.HeaderType_PEER_ADMIN_OPERATION, "", signer, msg, 0, 0)
		if err != nil {
			log.Panicf("Failed signing: %v", err)
		}
		return env
	}

	adminClient, err := peerCli.getAdmin()
	if err != nil {
		return "", err
	}
	status, err := adminClient.GetStatus(context.Background(), wrapEnv(&pb.AdminOperation{}))
	if err != nil {
		return "", err
	}
	return PeerStatus(status.Status.String()), nil
}
